package com.twu.biblioteca;

import static com.twu.biblioteca.BiblioMessages.*;
import static com.twu.biblioteca.OperationStatus.MAIN;

public class Biblioteca {
    private BookSystem bookSystem = new BookSystem();
    private Movies movies = new Movies();
    private OperationStatus currentStatus = MAIN;

    public String welcomeMessage() {
        return LOGIN_SUC + MAIN_MENU;
    }

    public String getCommand(String input) {
        String display = "";
        switch (currentStatus){
            case MAIN:
                display =  mainMenu(input);
                break;
            case CHECKOUT:
                display = checkoutBook(input);
                break;
            case CHECKOUT_MOVIE:
                display = checkoutMovie(input);
                break;
            case RETURN:
                display = returnBook(input);
                break;
            default:
                break;
        }
        return display;
    }

    private String mainMenu(String input) {
        String display = INVALID_OPTION + MAIN_MENU;
        try{
            int i = Integer.parseInt(input);
            switch (i) {
                case 1:
                    display = bookSystem.listBooks();
                    break;
                case 2:
                    display = bookSystem.listBooks() + GUIDE_CHECKOUT;
                    setStatus(OperationStatus.CHECKOUT);
                    break;
                case 3:
                    display = GUIDE_RETURN;
                    setStatus(OperationStatus.RETURN);
                    break;
                case 4:
                    display = movies.list();
                    break;
                case 5:
                    display = movies.list() + GUIDE_MOVIE_CHECKOUT;
                    setStatus(OperationStatus.CHECKOUT_MOVIE);
                    break;
                case 6:
                    display = userInfo();
                    break;
                default:
                    break;
            }
        } catch (Exception ignored){
        }
        return display;
    }

    private void setStatus(OperationStatus s) {
        currentStatus = s;
    }

    public String checkoutBook(String input) {
        setStatus(MAIN);
        return bookSystem.checkout(input) ? CHECKOUT_SUCCESS + BACK_TO_MENU : CHECKOUT_FAIL + BACK_TO_MENU;
    }

    public String returnBook(String input) {
        setStatus(MAIN);
        return bookSystem.returnBook(input) ? RETURN_SUCCESS + BACK_TO_MENU : RETURN_FAIL + BACK_TO_MENU;
    }

    public void add(Book book) {
        bookSystem.add(book);
    }

    public void add(Movie movie) {
        movies.add(movie);
    }

    public String checkoutMovie(String movieName) {
        setStatus(MAIN);
        return (movies.remove(movieName) ? CHECK_MOVIE_SUC : CHECK_MOVIE_FAIL)+BACK_TO_MENU;
    }

    public String loginMessage() {
        return WELCOME + LOGIN_GUIDE;
    }

    public boolean login(String input) {
        try {
            String[] params = input.split(SEPARATOR);
            String userName = params[0];
            String password = params[1];
            return userName.equals("123-4567") && password.equals("123");
        } catch (Exception e) {
            return false;
        }
    }

    public String userInfo() {
        return "Jam, jam@tw.com, 800800800";
    }
}
